﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using DataLayer.dbml;

namespace DataLayer.Controller
{
    /// <summary>
    /// Clase que expone los métodos que invocan a los web service de Sales Force
    /// </summary>
    public static class SFController
    {
        /// <summary>
        /// Método de autenticación ante el sistema de Sales Force
        /// </summary>
        /// <returns></returns>
        public static SForcePartner.LoginResult SFAuthentication()
        {
            string SFUser = ConfigurationManager.AppSettings["SFUser"].ToString();
            string SFPwd = ConfigurationManager.AppSettings["SFPwd"].ToString();

            SForcePartner.LoginResult lr = new SForcePartner.LoginResult();            
            SForcePartner.SforceService ss = new SForcePartner.SforceService();

            lr = ss.login(SFUser, SFPwd);

            //SForceService.LoginResult lr = new SForceService.LoginResult();        
            //SForceService.SforceService ss = new SForceService.SforceService();
            return lr;
        }

        /// <summary>
        /// Crea un caso
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static SForceCase.creacionCasoRespuesta_cls CrearCaso(SForceCase.parametrosCreacionCaso param)
        {
            SForceCase.wsCreacionCasoSFDC_clsService CService = new SForceCase.wsCreacionCasoSFDC_clsService();
            CService.SessionHeaderValue = new SForceCase.SessionHeader();
            CService.SessionHeaderValue.sessionId = SFController.SFAuthentication().sessionId;            

            return CService.creaCaso_ws(param);
        }

        /// <summary>
        /// Crea un comentario de caso
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static SForceCaseComment.creacionComentarioRespuesta_cls CrearComentarioCaso(SForceCaseComment.parametrosComentarioCaso param)
        {
            SForceCaseComment.wsCreacionComentarioCasoSFDC_clsService CCService = new SForceCaseComment.wsCreacionComentarioCasoSFDC_clsService();
            CCService.SessionHeaderValue = new SForceCaseComment.SessionHeader();
            CCService.SessionHeaderValue.sessionId = SFController.SFAuthentication().sessionId;

            return CCService.creaComentarioCaso_ws(param);
        }

        /// <summary>
        /// Cierra un caso
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static SForceCaseClose.creacionComentarioRespuesta_cls CerrarCaso(SForceCaseClose.parametrosComentarioCaso param)
        {
            SForceCaseClose.wsCierreCasoPortal_SFDC_clsService CCService = new DataLayer.SForceCaseClose.wsCierreCasoPortal_SFDC_clsService();
            CCService.SessionHeaderValue = new SForceCaseClose.SessionHeader();
            CCService.SessionHeaderValue.sessionId = SFController.SFAuthentication().sessionId;

            return CCService.cierraCasoSFDC_ws(param);
        }

    

        /// <summary>
        /// Crea una oportunidad
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static SForceOPService.creaOportunidadRespuesta_cls CrearOportunidad(SForceOPService.parametrosOportunidad param)
        {
            SForceOPService.wsCreacionoportunidaSFDC_clsService OPService = new SForceOPService.wsCreacionoportunidaSFDC_clsService();
            OPService.SessionHeaderValue = new SForceOPService.SessionHeader();
            OPService.SessionHeaderValue.sessionId = SFController.SFAuthentication().sessionId;

            return OPService.creaOportunidad_ws(param);
        }

        /// <summary>
        /// Actualiza una orden de trabajo
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static SForceOTService.ActualizarOrdenDeTrabajoRespuesta ActualizarOT(SForceOTService.ActualizarOrdenDeTrabajoParametros param)
        { 
            SForceOTService.OrdenDeTrabajoWebServiceService OTService = new SForceOTService.OrdenDeTrabajoWebServiceService();
            OTService.SessionHeaderValue = new SForceOTService.SessionHeader();
            OTService.SessionHeaderValue.sessionId = SFController.SFAuthentication().sessionId;

            return OTService.actualizarOrdenDeTrabajo(param);
        }

    }
}
